<template>
  <el-upload
    class="upload-demo"
    :action="baseUrl + '/question/upload'"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-remove="beforeRemove"
    :before-upload="beforeUploadVideo"
    multiple
    :limit="3"
    :on-exceed="handleExceed"
    :file-list="fileList"
    :on-success="handleAvatarSuccess"
  >
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">只能上传视频格式文件</div>
  </el-upload>
</template>
<script>
export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,

      fileList: []
    }
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    beforeUploadVideo(file) {
      const isLt30M = file.size / 1024 / 1024 < 30
      if (
        [
          'video/mp4',
          'video/ogg',
          'video/flv',
          'video/avi',
          'video/wmv',
          'video/rmvb'
        ].indexOf(file.type) == -1
      ) {
        this.$message.error('请上传正确的视频格式')
        return false
      }
      if (!isLt30M) {
        this.$message.error('上传视频大小不能超过30MB哦!')
        return false
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      )
    },
    beforeRemove(file, fileList) {
      console.log(file)
      if (
        [
          'video/mp4',
          'video/ogg',
          'video/flv',
          'video/avi',
          'video/wmv',
          'video/rmvb'
        ].indexOf(file.raw.type) == -1
      ) {
        return
      } else {
        return this.$confirm(`确定移除 ${file.name}？`)
      }
    },
    handleAvatarSuccess(res) {
      // console.log(res)
      this.$emit('input', this.baseUrl + '/' + res.data.url)
      this.$message.success('上传成功')
    }
  }
}
</script>
